<?php
namespace Admin\Controller;
use Think\Controller;
class HotelController extends Controller {
    public function hoteldetail(){
        $data=M('hotel')->select();
        $this->assign('data',$data);
        $this->display('hoteldetail');
    }
    //酒店详情设置处理器
    public function changedetail(){
        $data=array(
            'hotel_id'=>I('id'),
            'title'=>I('title'),
            'location'=>I('address'),
            'facilities'=>I('facilities'),
            'phone'=>I('phone'),
            'about'=>I('about')
        );
        $row=M('hotel')->data($data)->save();
        $code=null;
        if($row>0){
            $code['code']='0000';
            $code['message']='修改成功';
        }else{
            $code['code']='1001';
            $code['message']='修改未成功';
        }
        $this->ajaxReturn(json_encode($code),'json');
    }
    //分类页面加载
    public function category(){
        $this->display();
    }
    //分类数据加载
    public function category_load(){
        $data=M('hotel_category')->select();
        echo json_encode($data);
    }
    //分类增、删、改处理器
    public function category_handle(){
        $saveType=I('saveType');
        $code=null;
        if($saveType=='add'){
            //添加数据
            $data=array(
                'cat_id'=>'',
                'hotel_id'=>1,
                'title'=>I('title'),
                'enabled'=>I('enabled')
            );
            $id=M('hotel_category')->data($data)->add();
            if($id>0){
                $code['type']=$saveType;
                $code['saveData']=$data;
                $code['data']=array('id'=>$id);
            }
        }elseif($saveType=='update'){
            //更新数据
             $data=array(
                'cat_id'=>I('cat_id'),
                'hotel_id'=>1,
                'title'=>I('title'),
                'enabled'=>I('enabled')
            );
            $row=M('hotel_category')->data($data)->save();
            if($row>0){
                $code['type']=$saveType;
                $code['saveData']=$data;
                $code['data']='0000';
            }
        }else{
            //删除数据
            $id=I('ids');
            $row=M('hotel_category')->where(array('cat_id'=>array('in',$id)))->delete();
            if($row>0){
                $code['type']=$saveType;
                $code['saveData']=$data;
                $code['data']='0000';
            }
        }
        $this->ajaxReturn(json_encode($code),'json');
    }
    //加载房间添加页面
    public function room_add_show(){
        $category=M('hotel_category')->where(array('enabled'=>1))->select();
        $this->assign('data',$category);
        $this->display();
    }
    //富文本编辑器图片上传方法
    public function upload(){
        $upload=new \Think\Upload();
        $upload->maxSize=5*1024*1024;
        $upload->autosub=true;
        $upload->saveName=array('uniqid','');
        $upload->exts=array('jpg','gif','png','jpeg');
        $upload->rootPath='./Public/image/';
        $upload->savePath = ''; 
        $upload->subName=array('date','Ymd');
        $info=$upload->upload();
        if($info){
            foreach ($info as $file) {
                $file['filepath']=$file['savepath'].$file['savename'];
            }
            echo json_encode(array(
                'url'=>$file['filepath'],
                'title'=>htmlspecialchars($_POST['pictitle'],ENT_QUOTES),
                'original'=>$file['savename'],
                'state'=>'SUCCESS'
            ));
        }else{
            echo json_encode(array('state'=>$upload->getError())); 
        }
    }
    //酒店房间添加处理器
    public function room_add_handle(){
        $info=upload();
        $imgurl=$info['img']['savepath'].$info['img']['savename'];//获得上传的图片的地址
        $data=array(//构造数组
            'title'=>I('title'),
            'imgurl'=>$imgurl,
            'num'=>I('num'),
            'area'=>I('area'),
            'bed_standard'=>I('bed_standard'),
            'propertise'=>html_entity_decode(I('content')),
            'price'=>I('price'),
            'cprice'=>I('cprice'),
            'mprice'=>I('mprice'),
            'score'=>I('score'),
            'enabled'=>I('enabled'),
            'addtime'=>date('Y-m-d H:i:s',time()),
            'cat_id'=>I('cat_id')
        );

        M('hotel_room')->startTrans();//开始事务
        $room_id=M('hotel_room')->data($data)->add();
        $range=I('range');//获取商品的状态
        $state_data['parentid']=1;//设置父id
        $state_data['pro_id']=$room_id;//房间id
        foreach ($range as $value) {
              $state_data[$value]=1;//构造数组
         }
        $id=M('products_state')->data($state_data)->add();
        $serve_data['room_id']=$room_id;
        $serve_data['title']='null';
        $serve_data['content']='null';
        $serve_id=M('room_serve')->data($serve_data)->add();
        if($room_id && $id && $serve_id){//验证是否都完成
            M('hotel_room')->commit();//成功提交事务
            echo '<script>alert("添加成功~您还可以为房间添加专属服务")</script>';
             $category=M('hotel_category')->where(array('enabled'=>1))->select();
            $this->assign('data',$category);
            $this->display('Hotel/room_add_show');
        }else{
            M('hotel_room')->rollback();//否则回滚事务
            echo '<script>alert("添加失败，请重试~")</script>';
            $category=M('hotel_category')->where(array('enabled'=>1))->select();
            $this->assign('data',$category);
            $this->display('Hotel/room_add_show');
        }
    }
    ////room_set_show页面加载器
    public function room_set_show(){
         $category=M('hotel_category')->where(array('enabled'=>1))->select();
            $this->assign('data',$category);
        $this->display();
    }
    ////room_set_show数据加载
    public function room_load(){
        $start=I('start');
        $limit=I('limit');
        $title=I('room_name');
        $title='%'.$title.'%';
        $map['title']=array('like',$title);
        $data['rows']=M('hotel_room')->where($map)->limit($start, $limit)->select();
        $data['results']=M('hotel_room')->where($map)->count();
        $data['hasError']=false;
        $data['error']='';
        echo json_encode($data);
    }
    ////room_set_show取得指定id的详细数据
    public function room_detail_load(){
        $room_id=I('room_id');
        $data=M('hotel_room')->where(array('room_id'=>$room_id))->select();
        $data=$data[0];
        $this->ajaxReturn(json_encode($data),'json');
    }
    //room_set_show 的添加处理器
    public function room_set_handle(){
        $info=upload();
        $imgurl=$info['img']['savepath'].$info['img']['savename'];//获得上传的图片的地址
        $data=array(//构造数组
            'title'=>I('title'),
            'room_id'=>I('room_id'),
            'num'=>I('num'),
            'area'=>I('area'),
            'bed_standard'=>I('bed_standard'),
            'propertise'=>html_entity_decode(I('content')),
            'price'=>I('price'),
            'cprice'=>I('cprice'),
            'mprice'=>I('mprice'),
            'score'=>I('score'),
            'enabled'=>I('enabled'),
            'addtime'=>date('Y-m-d H:i:s',time()),
            'cat_id'=>I('cat_id')
        );
        if($imgurl!=""){
            $data['imgurl']=$imgurl;
        }
        $row=M('hotel_room')->data($data)->save();
        if($row>0){
            $category=M('hotel_category')->where(array('enabled'=>1))->select();
            echo '<script>alert("修改成功")</script>';
            $this->assign('data',$category);
            $this->display('Hotel/room_set_show');
        }else{
            $category=M('hotel_category')->where(array('enabled'=>1))->select();
            echo '<script>alert("修改失败，请重试")</script>';
            $this->assign('data',$category);
            $this->display('Hotel/room_set_show');
        }
    }
    //room_set_show 的删除处理器
    public function room_del_handle(){
        $id=I('ids');
        $row=M('hotel_room')->where(array('room_id'=>array('in',$id)))->delete();
        $data=null;
        if($row>0){
            $data['code']='0000';
            $data['message']='删除成功';
        }else{
            $data['code']='1001';
            $data['message']='删除失败，请重试';
        }
        $this->ajaxReturn(json_encode($data),'json');
    }
    //room_serve_show页面加载
    public function room_serve_show(){
        $this->display();
    }
    public function room_serve_load(){
        $start=I('start');
        $limit=I('limit');
        $room=M('hotel_room')->limit($start,$limit)->select();
        $data=null;
        for ($i=0; $i < count($room); $i++) { 
            $data[$i]['room_id']=$room[$i]['room_id'];
            $data[$i]['title']=$room[$i]['title'];
            $products_state=M('products_state')->where(array('pro_id'=>$room[$i]['room_id'],'parentid'=>1))->select();
            $data[$i]['is_new']= $products_state[0]['is_new'];
            $data[$i]['is_hot']= $products_state[0]['is_hot'];
            $data[$i]['is_sale']= $products_state[0]['is_sale'];
            $data[$i]['is_index']= $products_state[0]['is_index'];
            $room_serve=M('room_serve')->where(array('room_id'=>$room[$i]['room_id']))->select();
            $data[$i]['records']=$room_serve;
            //dump($products_state);
        }
        //dump($data);
        $code['rows']=$data;
        $code['results']=M('hotel_room')->where($map)->count();
        $code['hassError']=false;
        $code['error']='';
        echo json_encode($code);
    }
    public function room_serve_handle(){
        $saveType=I('saveType');
        $parentid=1;
        $pro_id=I('room_id');
        $code=null;
        if($saveType=='update'){
            $data=array(
                'is_new'=>I('is_new'),
                'is_hot'=>I('is_hot'),
                'is_sale'=>I('is_sale'),
                'is_index'=>I('is_index')
            );
            $row=M('products_state')->where(array('parentid'=>$parentid,'pro_id'=>$pro_id))->data($data)->save();
            if($row>0){
                $code['type']=$saveType;
                $code['saveData']=$data;
                $code['data']='0000';
            }else{
                $code['type']=$saveType;
                $code['saveData']=$data;
                $code['data']='1001';
            } 
        }
        $this->ajaxReturn(json_encode($code),'json');
    }
    public function serve_item_load(){
        $room_id=I('room_id');
        $data=M('room_serve')->where(array('room_id'=>$room_id))->select();
        $this->ajaxReturn(json_encode($data),'json');
    }
    public function serve_del_handle(){
        $serve_id=I('serve_id');
        $room_id=I('room_id');

        $count=M('room_serve')->where(array('room_id'=>$room_id))->count();
        $code=null;
        $row=null;
        if($count>1){
            $row=M('room_serve')->where(array('serve_id'=>$serve_id))->delete();
        }else{
            $data['serve_id']=$serve_id;
            $data['title']='null';
            $data['content']='null';
            $row=M('room_serve')->data($data)->save();
        }
        if($row>0){
            $code['code']='0000';
            $code['message']='删除成功';
        }else{
            $code['code']='1001';
            $code['message']='删除失败，请重试';
        }
        $this->ajaxReturn(json_encode($code),'json');
    }
    public function serve_add_handle(){
        $serve_id=I('serve_id');
        $room_id=I('room_id');
        $title=I('title');
        $content=I('content');
        $newdata=null;
        $olddata=null;
        //dump(I('POST.'));
        $new_index=0;
        $old_index=0;
        for($i=0;$i<count($title);$i++){
            if($serve_id[$i]==''){
                $newdata[$new_index]['room_id']=$room_id[$i];
                $newdata[$new_index]['title']=$title[$i];
                $newdata[$new_index]['content']=$content[$i];
                $new_index++;
            }else{
                $olddata[$old_index]['serve_id']=$serve_id[$i];
                $olddata[$old_index]['room_id']=$room_id[$i];
                $olddata[$old_index]['title']=$title[$i];
                $olddata[$old_index]['content']=$content[$i];
                $old_index++;
            }
        }
        $id=0;
        for($i=0;$i<count($olddata);$i++){
            $data=$olddata[$i];
            $row=M('room_serve')->data($data)->save();
            if($row>0){
                $id++;
            }
        }
        for($i=0;$i<count($newdata);$i++){
            $data=$newdata[$i];
            $row=M('room_serve')->data($data)->add();
            if($row>0){
                $id++;
            }
        }
        if($id>0){
            $row=M('room_serve')->where(array('title'=>'null'))->delete();
        }
        $this->ajaxReturn(json_encode($id),'json');
    }
}
?>